$(function() {
	
	var isAdd = true;
	
	window.operateEvents = {
		'click .label-primary' : function(e, value, row, index) {
			var roleId = row.roleId;
			$("input[name='roleId']").val(roleId);
			var setting = {
				check : {
					enable : true,
					nocheckInherit : true,
					chkboxType: { "Y": "p", "N": "s" }
				},
				showline : {
					enable : true
				},
				data : {
					simpleData : {
						enable : true
					}
				}
			};
			var url = "listTree?roleId=" + roleId;
			$.ajax({
				type : "POST",
				cache : false,
				dataType : 'json',
				url : url,
				success : function(data) {
					$.fn.zTree.init($("#treeDemo"), setting, data);
				}
			});
			$("#sabet").click(function() {
				var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
				var nodes = treeObj.getCheckedNodes(true);
				var ss = "";
				for ( var i = 0; i < nodes.length; i++) {
					if (ss == "") {
						ss = nodes[i].id;
					} else {
						ss = ss + "," + nodes[i].id;
					}
				}
				$("input[name='authId']").val(ss);
				var roleId = $("input[name='roleId']").val();
				$("#roleAuth").validate({
					submitHandler : function(form){
						$.post("addRoleAuth", {authId : ss,roleId : roleId}, function(data) {
							if(data.code == 200){
								layer.msg(data.msg, {icon : 1,time : 1500,});
								setTimeout(function(){
									$("#modal-edit1").modal('hide');
									$('#table').bootstrapTable("refresh");
								},2000);
							}else{
								layer.msg(data.msg, {icon : 2,time : 1500,});
							}
						}, "json");
					}
				});
			});
			$('#modal-edit1').modal('toggle');
		}
	};
	/**
	 * 新增框
	 */
	$("#addRoleForm").validate({
		errorElement : 'span',
		errorPlacement : function(error, element) {
			error.appendTo(element.parent());
		},
		rules : {
			roleName : {
				required : true,
				remote : {
					url : "checkRoleName",
					type : "post",
					dataType : "json",
					data : {
						roleName : function() {
							return $("input[name=roleName]").val();
						},
					}
				},
			},
			roleInfo : "required",
		},
		messages : {
			roleName : {
				required : "请输入角色名称",
				remote : "角色名称已存在",
			},
			roleInfo : "请输入备注",
		},
		submitHandler : function(form) { // 验证通过后的执行方法
			// 当前的form通过ajax方式提交（用到jQuery.Form文件）
			var url = isAdd ? "addRole" : "updateRole";
			var roleName = $("input[name=roleName]").val();
			var roleInfo = $("input[name=roleInfo]").val();
			var roleId = $("input[name=roleId]").val();
			$.post(url, {roleId : roleId,roleName : roleName,roleInfo : roleInfo}, function(data) {
				if(data.code == 200){
					layer.msg(data.msg, {icon : 1,time : 1500,});
					setTimeout(function(){
						$("#modal-add").modal('hide');
						$('#table').bootstrapTable("refresh");
					},2000);
				}else{
					layer.msg(data.msg, {icon : 2,time : 1500,});
				}
			}, "json");

		},

	});
/**
 * 修改框
 */
	$("#updateRoleForm").validate({
		errorElement : 'span',
		errorPlacement : function(error, element) {
			error.appendTo(element.parent());
		},
		rules : {
			roleName1 : {
				required : true,
				remote : {
					url : "checkUpdateRoleName",
					type : "post",
					dataType : "json",
					data : {
						roleId : function() {
							return $("#roleId1").val();
						},
					}
				},
			},
			roleInfo1 : "required",
		},
		messages : {
			roleName1 : {
				required : "请输入角色名称",
				remote : "角色名称已存在",
			},
			roleInfo1 : "请输入角色信息",
		},
		submitHandler : function(form) { // 验证通过后的执行方法
			// 当前的form通过ajax方式提交（用到jQuery.Form文件）

			var url = isAdd ? "addRole" : "updateRole";
			var roleName = $("input[name=roleName1]").val();
			var roleInfo = $("input[name=roleInfo1]").val();
			var roleId = $("#roleId1").val();
			$.post(url, {
				roleId : roleId,
				roleName : roleName,
				roleInfo : roleInfo
			}, function(data) {
				if(data.code == 200){
					layer.msg(data.msg, {icon : 1,time : 1500,});
					setTimeout(function(){
						$("#modal-edit").modal('hide');
						$('#table').bootstrapTable("refresh");
					},2000);
				}else{
					layer.msg(data.msg, {icon : 2,time : 1500,});
				}
			}, "json");
		},
	});
	
	$("#saveBtn").click(function (){
		$(this).attr("disabled",true);
		 $("#addRoleForm").submit(); 
	    setTimeout("$('#saveBtn').removeAttr('disabled')",3000);
	});
	
	$("#edit").click(function() {
		isAdd = false;
		var ids = getAllChecked();
		if (ids.length == 1) {
			var role = getIdSelections();
			$("input[name=roleName1]").val(role[0].roleName);
			$("input[name=roleInfo1]").val(role[0].roleInfo);
			$("input[name=roleId]").val(role[0].roleId);
			$('#modal-edit').modal('toggle');
		}else if(ids.length == 0){
			layer.msg("未选择数据");
		}else{
			layer.msg("请勿勾选多个");
		}
	});

	function getAllChecked (){
  		var ids = [];
  		$("input[type=checkbox]").each(
  			function (){
  				if(this.checked){
  					ids.push($(this).val());
  				}
  			}
  		);
  		return ids;
  	} 
	
	function getIdSelections() {
		return $.map($('#table').bootstrapTable('getSelections'),function(row) {
			return row;
		});
	}
	
	function clear(obj){
		$(obj).click(function(){
			location.href = "../a/sysrole"
		});
	}
	clear(".close");
	clear(".closeBtn");
});
